package com.suke.czx.modules.info.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.info.entity.Info;
import com.suke.czx.modules.info.entity.InfoSearchBo;
import com.suke.czx.modules.info.mapper.InfoMapper;
import com.suke.czx.modules.info.service.InfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 信息
 * 代码描述: 信息业务层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/01 22:46
 */
@Service
@AllArgsConstructor
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {

    private final InfoMapper infoMapper;

    /**
     * 分页查询信息列表
     * @param infoSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pageInfoList(InfoSearchBo infoSearchBo) {
        Page<Info> page = PageHelper.startPage(infoSearchBo.getPage(), infoSearchBo.getLimit(), true,true,true);
        // 获取信息查询结果集
        List<Info> infoList = infoMapper.pageInfoList(infoSearchBo);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal())).setData(Optional.ofNullable(infoList).orElse(Lists.newArrayList()));
    }

    /**
     * 删除信息
     * @param infoId 信息编码
     * @param userId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelInfo(Long infoId, Long userId) {
        infoMapper.updateDelInfo(infoId, userId);
        return R.ok();
    }

}
